package dj.common.support;

import com.dj.common.constants.ResponseStatus;
import com.dj.common.lang.PageResult;
import com.dj.common.response.CentralPageResponse;
import org.springframework.data.domain.Page;

import java.util.List;


/**
 * <br>
 * <p>中心部件响应数据分页转换处理handle</p>
 *
 * <br>
 * @author ZJG
 * @version 1.0
 * @date 2021/7/10 下午3:06
 */
public final class ResponsePageHandler {

    /**
     * 中心部件分页转换
     * {@link CentralPageResponse}
     *
     * @param result
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralPageResponse<TDest> toPageResponse(PageResult<TSrc> result, ResponseConverter<TSrc, TDest> converter) {
        if (null != result) {
            return toPageResponse(result.getPageNum(), result.getPageSize(), result.getTotalCount(), result.getDataList(), converter);
        } else {
            return new CentralPageResponse<>();
        }
    }

    /**
     * 中心部件分页转换
     * {@link CentralPageResponse}
     *
     * @param pageNum
     * @param pageSize
     * @param totalCount
     * @param result
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralPageResponse<TDest> toPageResponse(int pageNum,
                                                                          int pageSize,
                                                                          int totalCount,
                                                                          List<TSrc> result,
                                                                          ResponseConverter<TSrc, TDest> converter) {
        final CentralPageResponse<TDest> response = new CentralPageResponse<>();
        response.setPageNum(pageNum);
        response.setPageSize(pageSize);
        response.setTotalCount(totalCount);
        response.setData(ResponseListHandler.convert(result, converter));
        return response;
    }

    /**
     * 中心部件分页转换
     * {@link CentralPageResponse}
     *
     * @param pageNum
     * @param pageSize
     * @param totalCount
     * @param result
     * @param <TDest>
     * @return
     */
    public static <TDest> CentralPageResponse<TDest> toPageResponse(int pageNum,
                                                                    int pageSize,
                                                                    int totalCount,
                                                                    List<TDest> result) {
        final CentralPageResponse<TDest> response = new CentralPageResponse<>();
        response.setPageNum(pageNum);
        response.setPageSize(pageSize);
        response.setTotalCount(totalCount);
        response.setData(ResponseListHandler.emptyIfNull(result));
        return response;
    }

    /**
     * 中心部件分页转换
     * {@link CentralPageResponse}
     *
     * @param source
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralPageResponse<TDest> toPageResponse(final CentralPageResponse<TSrc> source,
                                                                          final ResponseConverter<TSrc, TDest> converter) {
        return null != source ?
                toPageResponse(source.getPageNum(), source.getPageSize(), source.getTotalCount(), source.getData(), converter) :
                new CentralPageResponse<TDest>();
    }

    /**
     * 中心部件分页转换
     *      适配org.springframework.data.domain.Page
     * {@link CentralPageResponse}
     *
     * @param page          {@link Page}
     * @param converter
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T, V> CentralPageResponse<V> toPageResponse(final Page<T> page,
                                                               ResponseConverter<T, V> converter) {
        return toPageResponse(page.getNumber(),
                page.getSize(),
                (int) page.getTotalElements(),
                ResponseListHandler.convert(page.getContent(), converter));
    }

    /**
     * 中心部件分页转换
     *      适配mybatis分页处理
     * @param page         {@link  com.baomidou.mybatisplus.extension.plugins.pagination.Page}
     * @param converter
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T, V> CentralPageResponse<V> toMybatisPageResponse(final com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> page,
                                                               ResponseConverter<T, V> converter) {
        return toPageResponse(Long.valueOf(page.getCurrent()).intValue(),
                Long.valueOf(page.getSize()).intValue(),
                Long.valueOf(page.getTotal()).intValue(),
                ResponseListHandler.convert(page.getRecords(), converter));
    }


    /**
     * 处理失败
     * 返回无提示信息的中心部件response对象
     * {@link CentralPageResponse}
     *
     * @return
     */
    public static <TDest> CentralPageResponse<TDest> fail() {
        final CentralPageResponse<TDest> response = new CentralPageResponse<>();
        response.setCode(500);
        response.setMsg("request is failure.");
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示msg的中心部件response对象
     * {@link CentralPageResponse}
     *
     * @param message
     * @return
     */
    public static <TDest> CentralPageResponse<TDest> fail(String message) {
        final CentralPageResponse<TDest> response = new CentralPageResponse<>();
        response.setCode(500);
        response.setMsg(message);
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示code&msg的中心部件response对象
     * {@link CentralPageResponse}
     *
     * @param code
     * @param message
     * @return
     */
    public static <TDest> CentralPageResponse<TDest> fail(int code, String message) {
        code = code == 0? ResponseStatus.HTTP_INTERNAL_ERROR : code;
        final CentralPageResponse<TDest> response = new CentralPageResponse<>();
        response.setCode(code);
        response.setMsg(message);
        return response;
    }


}
